package com.paireach.szwolaiyun.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.fragmentall.DcQueueOutSideClickButtonFragment;
import com.paireach.szwolaiyun.receiver.StatusJumpReceiver;
import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;

/**
 * 参考设计图E界面，主要功能：
 * 用于处理DC进场的一个超时机制，我把这个单独拿出来作为一个界面来写，虽然可能有点繁琐，但好处是逻辑更加清晰，跳转不容易发生错误，而且也减少程序编写的难度
 * <br/>无论是请求成功还是请求失败，都会跳转到B界面，请求结果就是给用户看看的
 * <br/>跳转到B界面后会请求完整的对象然后跳转到D界面，再由D界面来加载fragment
 */
public class Activity_E extends BaseActivity {
    private ImageView backView;
    private TextView tvTitleView;
    private String orderNumber;
    private StatusJumpReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity__e);
        initData();
        initView();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        DcQueueOutSideClickButtonFragment fragment = DcQueueOutSideClickButtonFragment.getInstance(orderNumber);
        transaction.replace(R.id.frame, fragment, DcQueueOutSideClickButtonFragment.TAG).commit();
    }

    private void initData() {
        orderNumber = getIntent().getStringExtra("orderNumber");
        receiver = new StatusJumpReceiver(this);
    }

    private void initView() {
        backView = (ImageView) findViewById(R.id.layout_top_back);
        tvTitleView = (TextView) findViewById(R.id.layout_top_change);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity_E.this.finish();
            }
        });
        tvTitleView.setText("DC入场提示");
    }

    public void startAc(Intent intent) {
        //上面的有可能没有销毁E，
        Activity_E.this.finish();
        startActivity(intent);
//        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.paireach.szwolaiyun.ACTION_JUMP");
        registerReceiver(receiver, filter);
    }

    private class StatusJumpReceiver extends BroadcastReceiver {

        private String action = "com.paireach.szwolaiyun.ACTION_JUMP";
        private String orderNumber;
        private Activity mContext;

        public StatusJumpReceiver(Activity mContext) {
            this.mContext = mContext;
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(action)) {
                mContext.removeStickyBroadcast(new Intent("com.paireach.szwolaiyun.ACTION_JUMP"));
                //解析数据
                Bundle bundle = intent.getExtras();
                if (bundle != null) {
                    orderNumber = bundle.getString("orderCode");
                    Intent intent1 = new Intent(Activity_E.this, Activity_B.class);
                    intent1.putExtra("orderNumber", orderNumber);
                    //上面的有可能没有销毁E，
                    Activity_E.this.finish();
                    startActivity(intent1);
//                    finish();
                }
            }
        }
    }
}
